package se.energimyndigheten.lampguiden.info;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import se.energimyndigheten.lampguiden.R;
import se.energimyndigheten.lampguiden.SwappableListFragment;


public class InfoListFragment extends SwappableListFragment implements OnItemClickListener {

    private static final String TAG = InfoListFragment.class.getSimpleName();

    // list titles
    private static final Integer[] INFO_LIST = new Integer[]{R.string.info_types, R.string.info_definitions,
            R.string.info_recycling, R.string.info_legal, R.string.info_package,
            R.string.info_why_cancel, R.string.info_energy, R.string.info_about, R.string.info_energimyndigheten};

    // list icons
    private static final int[] DRAWABLE_LIST = new int[]{R.drawable.ic_info_types_circle, R.drawable.ic_info_definitions_circle,
            R.drawable.ic_info_recycling_circle, R.drawable.ic_info_legal_circle, R.drawable.ic_info_package_circle, R.drawable.ic_info_why_cancel_circle,
            R.drawable.ic_info_energy_circle, R.drawable.ic_info_about_circle, R.drawable.ic_info_energimyndigheten_circle};


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.info_activity, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        setListAdapter(new InfoAdapter());
        getListView().setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        try {
                InfoWebFragment iFrag = new InfoWebFragment();
                Bundle args = new Bundle();
                args.putString(InfoWebFragment.EXTRA_TITLE, getString(INFO_LIST[position]));
                args.putInt(InfoWebFragment.EXTRA_VIEW, INFO_LIST[position]);
                iFrag.setArguments(args);
                swapFragment(iFrag, true);
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        }
    }

    /*
     * Custom adapter to get list of titles and icons
     */
    class InfoAdapter extends ArrayAdapter<String> {

        public InfoAdapter() {
            super(getActivity(), R.layout.info_listitem, R.id.listitem);
            // add items
            for (int i = 0; i < INFO_LIST.length; i++) {
                add(getString(INFO_LIST[i]));
            }
            notifyDataSetChanged();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // recycle
            View row = super.getView(position, convertView, parent);
            // inflate
            if (row == null) {
                LayoutInflater inflater = getLayoutInflater(null); //added null as parameter
                row = inflater.inflate(R.layout.info_listitem, parent, false);
            }
            // update list item
            TextView listItem = (TextView) row.findViewById(R.id.listitem);
            listItem.setText(getString(INFO_LIST[position]));
            listItem.setCompoundDrawablesWithIntrinsicBounds(DRAWABLE_LIST[position], 0, 0, 0);
            listItem.setPadding(0, 0, 0, 0);

            return row;
        }
    }
}
